package com.sss.demo.web.controller;

import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import com.sss.demo.GlobalConstants;
import com.sss.demo.db.pojo.UserVo;
import com.sss.demo.web.methodLog;
import com.sss.demo.web.service.IUserService;

@Controller
@RequestMapping("user")
public class UserController {

	@Autowired
	private IUserService userService;
	
	@methodLog(operateDesc="request /user/allusers",operateType="requestURL")
	@RequestMapping(value="/allusers",method=RequestMethod.GET)
	@ResponseBody
	public List<UserVo> showUsers(HttpServletRequest request){
		UserVo user=new UserVo();
		user.setMail("Tommy@sina.com");
		user.setName("Tommy");
		request.getSession().setAttribute(GlobalConstants.SESSION_ACTOR_KEY, user);
		List<UserVo> userList = userService.getAllUsers();
		return userList;
	}
	
	@methodLog(operateDesc="request /user/changelang",operateType="requestURL")
	@RequestMapping(value="/changelang",method=RequestMethod.GET)
	public String changeLang(@RequestParam("lang")String language,HttpServletRequest request,HttpServletResponse response){
		language=language.toLowerCase();
        if(language==null||language.equals("")){
        	return "index";
        }else{
            if(language.equals("zh_cn")){
                Locale locale=new Locale("zh","CN");
                request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME , locale);
           
            }else if(language.equals("en_us")){
            	Locale locale = new Locale("en", "US"); 

                request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale);
            }
            return "index";
        }

	}
}
